// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Xml;

namespace SnapObjects.Data
{
    internal class XmlTemplateDataFormater : TemplateDataFormater
    {
        public override object Format(object value)
        {
            if (value is DateTime dateTime)
            {
                return dateTime.ToString("s");
            }
            else if (value is string str)
            {
                return System.Security.SecurityElement.Escape(str);
            }
            else if (value is byte[] bytes)
            {
                return Convert.ToBase64String(bytes);
            }
            else if (value is DateTimeOffset dto)
            {
                return dto.ToString("yyyy-MM-ddThh:mm:sszzz");
            }

            return value;
        }

        public override string FormatRaw(string raw)
        {
            var doc = new XmlDocument();

            doc.LoadXml(raw);

            return doc.GetIndentedXml();
        }
    }
}
